{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "ename": "ImportError",
     "evalue": "C extension: None not built. If you want to import pandas from the source directory, you may need to run 'python setup.py build_ext' to build the C extensions first.",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mImportError\u001b[0m                               Traceback (most recent call last)",
      "\u001b[0;32m~/.local/lib/python3.10/site-packages/pandas/__init__.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m     22\u001b[0m     \u001b[0;31m# numpy compat\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 23\u001b[0;31m     from pandas.compat import (\n\u001b[0m\u001b[1;32m     24\u001b[0m         \u001b[0mis_numpy_dev\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0m_is_numpy_dev\u001b[0m\u001b[0;34m,\u001b[0m  \u001b[0;31m# pyright: ignore[reportUnusedImport] # noqa: F401,E501\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/.local/lib/python3.10/site-packages/pandas/compat/__init__.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m     25\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mpandas\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcompat\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcompressors\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 26\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0mpandas\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcompat\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnumpy\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mis_numpy_dev\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     27\u001b[0m from pandas.compat.pyarrow import (\n",
      "\u001b[0;32m~/.local/lib/python3.10/site-packages/pandas/compat/numpy/__init__.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m     18\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0m_nlv\u001b[0m \u001b[0;34m<\u001b[0m \u001b[0mVersion\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0m_min_numpy_ver\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 19\u001b[0;31m     raise ImportError(\n\u001b[0m\u001b[1;32m     20\u001b[0m         \u001b[0;34mf\"this version of pandas is incompatible with numpy < {_min_numpy_ver}\\n\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mImportError\u001b[0m: this version of pandas is incompatible with numpy < 1.22.4\nyour numpy version is 1.21.5.\nPlease upgrade numpy to >= 1.22.4 to use this pandas version",
      "\nThe above exception was the direct cause of the following exception:\n",
      "\u001b[0;31mImportError\u001b[0m                               Traceback (most recent call last)",
      "\u001b[0;32m/tmp/ipykernel_16096/3869798782.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0mpymilvus\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mconnections\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mCollection\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      2\u001b[0m connections.connect(\n\u001b[1;32m      3\u001b[0m   \u001b[0malias\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m\"default\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      4\u001b[0m   \u001b[0mhost\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'localhost'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      5\u001b[0m   \u001b[0mport\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'19530'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/.local/lib/python3.10/site-packages/pymilvus/__init__.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m     21\u001b[0m     \u001b[0mBulkFileType\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     22\u001b[0m )\n\u001b[0;32m---> 23\u001b[0;31m from .bulk_writer.local_bulk_writer import (\n\u001b[0m\u001b[1;32m     24\u001b[0m     \u001b[0mLocalBulkWriter\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     25\u001b[0m )\n",
      "\u001b[0;32m~/.local/lib/python3.10/site-packages/pymilvus/bulk_writer/local_bulk_writer.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m     19\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mtyping\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mCallable\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mOptional\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     20\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 21\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0mpymilvus\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0morm\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mschema\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mCollectionSchema\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     22\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     23\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0;34m.\u001b[0m\u001b[0mbulk_writer\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mBulkWriter\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/.local/lib/python3.10/site-packages/pymilvus/orm/schema.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m     14\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mtyping\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mAny\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mDict\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mList\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mOptional\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mUnion\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     15\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 16\u001b[0;31m \u001b[0;32mimport\u001b[0m \u001b[0mpandas\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mpd\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     17\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mpandas\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mapi\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtypes\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mis_list_like\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     18\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/.local/lib/python3.10/site-packages/pandas/__init__.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m     26\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mImportError\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0m_err\u001b[0m\u001b[0;34m:\u001b[0m  \u001b[0;31m# pragma: no cover\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     27\u001b[0m     \u001b[0m_module\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m_err\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 28\u001b[0;31m     raise ImportError(\n\u001b[0m\u001b[1;32m     29\u001b[0m         \u001b[0;34mf\"C extension: {_module} not built. If you want to import \"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     30\u001b[0m         \u001b[0;34m\"pandas from the source directory, you may need to run \"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mImportError\u001b[0m: C extension: None not built. If you want to import pandas from the source directory, you may need to run 'python setup.py build_ext' to build the C extensions first."
     ]
    }
   ],
   "source": [
    "from pymilvus import connections,Collection\n",
    "connections.connect(\n",
    "  alias=\"default\", \n",
    "  host='localhost', \n",
    "  port='19530'\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'你好！有什么我可以帮助你的吗？'"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import os\n",
    "import openai\n",
    "openai.api_type = \"azure\"\n",
    "openai.api_base = \"https://us1.openai.azure.com/\"\n",
    "openai.api_version = \"2023-03-15-preview\"\n",
    "openai.api_key = \"64aae82617224549ab00d48e6d80e662\"\n",
    "\n",
    "response = openai.ChatCompletion.create(\n",
    "  engine=\"GPT35\",\n",
    "  messages = [{\"role\":\"user\",\"content\":\"你好\"}],\n",
    "  temperature=0.7,\n",
    "  max_tokens=800,\n",
    "  top_p=0.95,\n",
    "  frequency_penalty=0,\n",
    "  presence_penalty=0,\n",
    "  stop=None)\n",
    "response['choices'][0]['message']['content']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pymilvus import CollectionSchema, FieldSchema, DataType\n",
    "paragraph_id = FieldSchema(\n",
    "  name=\"paragraph_id\", \n",
    "  dtype=DataType.INT64, \n",
    "  is_primary=True\n",
    ")\n",
    "\n",
    "article_id = FieldSchema(\n",
    "  name=\"article_id\", \n",
    "  dtype=DataType.INT64\n",
    ")\n",
    "\n",
    "word_count = FieldSchema(\n",
    "  name=\"word_count\", \n",
    "  dtype=DataType.INT64\n",
    ")\n",
    "paragraph_vector = FieldSchema(\n",
    "  name=\"paragraph_vector\", \n",
    "  dtype=DataType.FLOAT_VECTOR, \n",
    "  dim=384\n",
    ")\n",
    "schema = CollectionSchema(\n",
    "  fields=[paragraph_id,article_id, word_count,paragraph_vector], \n",
    "  description=\"Paragraph search\"\n",
    ")\n",
    "collection_name = \"article_paragraph\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "collection = Collection(\n",
    "    name=collection_name, \n",
    "    schema=schema, \n",
    "    using='default', \n",
    "    shards_num=8,\n",
    "    consistency_level=\"Strong\"\n",
    "    )\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 生成向量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sentence_transformers import SentenceTransformer\n",
    "model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "sentences = \"This is an example sentence\"\n",
    "embeddings = model.encode(sentences)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# sentences = \"This is an example sentence\"\n",
    "\n",
    "# model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')\n",
    "# embeddings = model.encode(sentences)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 6.76569566e-02,  6.34959787e-02,  4.87130918e-02,  7.93049783e-02,\n",
       "        3.74480858e-02,  2.65282486e-03,  3.93749774e-02, -7.09842378e-03,\n",
       "        5.93613759e-02,  3.15370113e-02,  6.00980595e-02, -5.29051833e-02,\n",
       "        4.06067818e-02, -2.59308331e-02,  2.98428405e-02,  1.12686504e-03,\n",
       "        7.35148340e-02, -5.03818206e-02, -1.22386597e-01,  2.37028431e-02,\n",
       "        2.97265854e-02,  4.24768478e-02,  2.56337505e-02,  1.99516467e-03,\n",
       "       -5.69190793e-02, -2.71598548e-02, -3.29035558e-02,  6.60248622e-02,\n",
       "        1.19007185e-01, -4.58791181e-02, -7.26214573e-02, -3.25840078e-02,\n",
       "        5.23413904e-02,  4.50553112e-02,  8.25299323e-03,  3.67024429e-02,\n",
       "       -1.39414957e-02,  6.53919056e-02, -2.64272261e-02,  2.06424855e-04,\n",
       "       -1.36643872e-02, -3.62810604e-02, -1.95044000e-02, -2.89737824e-02,\n",
       "        3.94270904e-02, -8.84090737e-02,  2.62428448e-03,  1.36713590e-02,\n",
       "        4.83062230e-02, -3.11566219e-02, -1.17329188e-01, -5.11690751e-02,\n",
       "       -8.85287672e-02, -2.18963046e-02,  1.42986849e-02,  4.44168039e-02,\n",
       "       -1.34815592e-02,  7.43392259e-02,  2.66382787e-02, -1.98762696e-02,\n",
       "        1.79191902e-02, -1.06052486e-02, -9.04262811e-02,  2.13269014e-02,\n",
       "        1.41204864e-01, -6.47173636e-03, -1.40381407e-03, -1.53609524e-02,\n",
       "       -8.73572379e-02,  7.22174346e-02,  2.01402940e-02,  4.25587595e-02,\n",
       "       -3.49013619e-02,  3.19533778e-04, -8.02970752e-02, -3.27471793e-02,\n",
       "        2.85268296e-02, -5.13657480e-02,  1.09389193e-01,  8.19327831e-02,\n",
       "       -9.84039977e-02, -9.34096202e-02, -1.51292142e-02,  4.51248772e-02,\n",
       "        4.94172350e-02, -2.51868013e-02,  1.57077014e-02, -1.29290715e-01,\n",
       "        5.31888939e-03,  4.02341830e-03, -2.34571733e-02, -6.72982782e-02,\n",
       "        2.92280484e-02, -2.60845721e-02,  1.30625088e-02, -3.11663151e-02,\n",
       "       -4.82713543e-02, -5.58859669e-02, -3.87505069e-02,  1.20010845e-01,\n",
       "       -1.03924489e-02,  4.89705242e-02,  5.53537309e-02,  4.49357815e-02,\n",
       "       -4.00976231e-03, -1.02959722e-01, -2.92968843e-02, -5.83402514e-02,\n",
       "        2.70472597e-02, -2.20169406e-02, -7.22241253e-02, -4.13870141e-02,\n",
       "       -1.93297993e-02,  2.73325783e-03,  2.77000334e-04, -9.67588946e-02,\n",
       "       -1.00574672e-01, -1.41922813e-02, -8.07891712e-02,  4.53925431e-02,\n",
       "        2.45041270e-02,  5.97613715e-02, -7.38184899e-02,  1.19843995e-02,\n",
       "       -6.63403496e-02, -7.69044980e-02,  3.85158099e-02, -5.59362183e-33,\n",
       "        2.80013531e-02, -5.60785159e-02, -4.86601554e-02,  2.15569194e-02,\n",
       "        6.01980388e-02, -4.81402650e-02, -3.50247584e-02,  1.93314292e-02,\n",
       "       -1.75152086e-02, -3.89210507e-02, -3.81060853e-03, -1.70287751e-02,\n",
       "        2.82100178e-02,  1.28290821e-02,  4.71600667e-02,  6.21030554e-02,\n",
       "       -6.43589124e-02,  1.29285678e-01, -1.31231109e-02,  5.23069501e-02,\n",
       "       -3.73681299e-02,  2.89094504e-02, -1.68981366e-02, -2.37330664e-02,\n",
       "       -3.33491862e-02, -5.16762733e-02,  1.55357011e-02,  2.08802391e-02,\n",
       "       -1.25372149e-02,  4.59578857e-02,  3.72719690e-02,  2.80566625e-02,\n",
       "       -5.90004846e-02, -1.16988486e-02,  4.92182598e-02,  4.70329076e-02,\n",
       "        7.35487267e-02, -3.70530188e-02,  3.98462871e-03,  1.06412144e-02,\n",
       "       -1.61481919e-04, -5.27166352e-02,  2.75927931e-02, -3.92921045e-02,\n",
       "        8.44717622e-02,  4.86860648e-02, -4.85875783e-03,  1.79948602e-02,\n",
       "       -4.28569540e-02,  1.23375161e-02,  6.39957003e-03,  4.04823571e-02,\n",
       "        1.48886712e-02, -1.53940972e-02,  7.62948543e-02,  2.37043761e-02,\n",
       "        4.45237085e-02,  5.08196093e-02, -2.31252052e-03, -1.88736524e-02,\n",
       "       -1.23335915e-02,  4.66002151e-02, -5.63437752e-02,  6.29927069e-02,\n",
       "       -3.15535516e-02,  3.24912071e-02,  2.34673340e-02, -6.55437931e-02,\n",
       "        2.01709215e-02,  2.57082321e-02, -1.23869060e-02, -8.36491212e-03,\n",
       "       -6.64377362e-02,  9.43073630e-02, -3.57093252e-02, -3.42483111e-02,\n",
       "       -6.66356087e-03, -8.01519398e-03, -3.09711061e-02,  4.33012359e-02,\n",
       "       -8.21398012e-03, -1.50795057e-01,  3.07692401e-02,  4.00719084e-02,\n",
       "       -3.79293263e-02,  1.93214719e-03,  4.00530621e-02, -8.77075419e-02,\n",
       "       -3.68490629e-02,  8.57963134e-03, -3.19251716e-02, -1.25257624e-02,\n",
       "        7.35540092e-02,  1.34738372e-03,  2.05918662e-02,  2.71098274e-33,\n",
       "       -5.18576838e-02,  5.78361452e-02, -9.18985307e-02,  3.94421071e-02,\n",
       "        1.05576493e-01, -1.96911842e-02,  6.18402734e-02, -7.63465017e-02,\n",
       "        2.40880791e-02,  9.40048546e-02, -1.16535470e-01,  3.71198393e-02,\n",
       "        5.22425063e-02, -3.95859499e-03,  5.72214425e-02,  5.32855187e-03,\n",
       "        1.24016888e-01,  1.39022404e-02, -1.10250050e-02,  3.56053337e-02,\n",
       "       -3.30754817e-02,  8.16574618e-02, -1.52004091e-02,  6.05585054e-02,\n",
       "       -6.01397119e-02,  3.26102450e-02, -3.48296911e-02, -1.69881564e-02,\n",
       "       -9.74907279e-02, -2.71484759e-02,  1.74713728e-03, -7.68981650e-02,\n",
       "       -4.31857966e-02, -1.89985037e-02, -2.91661117e-02,  5.77488132e-02,\n",
       "        2.41821930e-02, -1.16902562e-02, -6.21435009e-02,  2.84352023e-02,\n",
       "       -2.37527158e-04, -2.51783542e-02,  4.39641159e-03,  8.12839493e-02,\n",
       "        3.64184417e-02, -6.04006425e-02, -3.65517475e-02, -7.93748572e-02,\n",
       "       -5.08525595e-03,  6.69698790e-02, -1.17784381e-01,  3.23743224e-02,\n",
       "       -4.71252613e-02, -1.34459725e-02, -9.48444605e-02,  8.24953243e-03,\n",
       "       -1.06748641e-02, -6.81881383e-02,  1.11816742e-03,  2.48020180e-02,\n",
       "       -6.35889396e-02,  2.84493119e-02, -2.61303857e-02,  8.58110934e-02,\n",
       "        1.14682280e-01, -5.35346009e-02, -5.63588738e-02,  4.26009595e-02,\n",
       "        1.09454244e-02,  2.09579226e-02,  1.00131124e-01,  3.26050818e-02,\n",
       "       -1.84208736e-01, -3.93208228e-02, -6.91455677e-02, -6.38105199e-02,\n",
       "       -6.56385720e-02, -6.41251821e-03, -4.79612388e-02, -7.68133402e-02,\n",
       "        2.95384601e-02, -2.29948219e-02,  4.17036898e-02, -2.50048060e-02,\n",
       "       -4.54509910e-03, -4.17136252e-02, -1.32289706e-02, -6.38357550e-02,\n",
       "       -2.46474496e-03, -1.37337837e-02,  1.68976802e-02, -6.30398542e-02,\n",
       "        8.98881033e-02,  4.18170653e-02, -1.85687579e-02, -1.80442150e-08,\n",
       "       -1.67998187e-02, -3.21577750e-02,  6.30383566e-02, -4.13091742e-02,\n",
       "        4.44818959e-02,  2.02463940e-03,  6.29592836e-02, -5.17369527e-03,\n",
       "       -1.00444648e-02, -3.05640213e-02,  3.52672786e-02,  5.58581874e-02,\n",
       "       -4.67125252e-02,  3.45103294e-02,  3.29578109e-02,  4.30114381e-02,\n",
       "        2.94361170e-02, -3.03164981e-02, -1.71107668e-02,  7.37484545e-02,\n",
       "       -5.47910072e-02,  2.77515538e-02,  6.20167051e-03,  1.58800930e-02,\n",
       "        3.42978425e-02, -5.15752845e-03,  2.35079844e-02,  7.53135383e-02,\n",
       "        1.92843545e-02,  3.36196646e-02,  5.09103611e-02,  1.52497113e-01,\n",
       "        1.64207574e-02,  2.70528477e-02,  3.75162028e-02,  2.18553096e-02,\n",
       "        5.66334426e-02, -3.95746343e-02,  7.12313727e-02, -5.41377515e-02,\n",
       "        1.03765936e-03,  2.11853161e-02, -3.56309675e-02,  1.09016962e-01,\n",
       "        2.76535004e-03,  3.13997641e-02,  1.38423452e-03, -3.45738903e-02,\n",
       "       -4.59277891e-02,  2.88082883e-02,  7.16900872e-03,  4.84684967e-02,\n",
       "        2.61018649e-02, -9.44069400e-03,  2.82169450e-02,  3.48724388e-02,\n",
       "        3.69098485e-02, -8.58947262e-03, -3.53205279e-02, -2.47857478e-02,\n",
       "       -1.91921070e-02,  3.80707681e-02,  5.99653795e-02, -4.22286987e-02],\n",
       "      dtype=float32)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "embeddings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "生成的UUID： 04a2022b-61f6-4bdc-b60d-4e0a365e4482\n",
      "转换为int型： 6158108076170300937328342041490244738\n"
     ]
    }
   ],
   "source": [
    "import uuid\n",
    "\n",
    "# 生成UUID\n",
    "generated_uuid = uuid.uuid4()\n",
    "\n",
    "# 将UUID转换为32位的int\n",
    "uuid_int = int(generated_uuid.int)\n",
    "\n",
    "print(\"生成的UUID：\", generated_uuid)\n",
    "print(\"转换为int型：\", uuid_int)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "paragraph_id = article_id = 33333"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "paragraph_vector = model.encode(\"你好世界，我想吃饭\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pymilvus import Collection\n",
    "collection = Collection(\"article_paragraph\")      # Get an existing collection.\n",
    "data = [[paragraph_id], [article_id],[0],[paragraph_vector]]\n",
    "mr = collection.insert(data)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "index_params = {\n",
    "  \"metric_type\":\"L2\",\n",
    "  \"index_type\":\"IVF_FLAT\",\n",
    "  \"params\":{\"nlist\":4096}\n",
    "}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Status(code=0, message='')"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from pymilvus import Collection, connections\n",
    "collection = Collection(\"article_paragraph\")      # Get an existing collection.\n",
    "collection.create_index(\n",
    "  field_name=\"paragraph_vector\", \n",
    "  index_params=index_params\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pymilvus import Collection\n",
    "collection = Collection(\"article_paragraph\")      # Get an existing collection.\n",
    "collection.load()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pymilvus import CollectionSchema, FieldSchema, DataType, Collection, connections\n",
    "\n",
    "\n",
    "class milvus_muster:\n",
    "    def __init__(self, host: str, port: str, alias: str, collection_name: str = \"article_paragraph\"):\n",
    "        connections.connect(\n",
    "            alias=alias,\n",
    "            host=host,\n",
    "            port=port\n",
    "        )\n",
    "\n",
    "        self.index_params = {\n",
    "            \"metric_type\": \"L2\",\n",
    "            \"index_type\": \"IVF_FLAT\",\n",
    "            \"params\": {\"nlist\": 1024}\n",
    "        }\n",
    "        self.collection_name = collection_name\n",
    "        self.collection = Collection(self.collection_name)  # Get an existing collection.\n",
    "        self.collection.create_index(\n",
    "            field_name=\"paragraph_vector\",\n",
    "            index_params=self.index_params\n",
    "        )\n",
    "\n",
    "        self.collection.load()\n",
    "        self.search_params = {\"metric_type\": \"L2\", \"params\": {\"nprobe\": 10}}\n",
    "\n",
    "    async def insert_vector(self, paragraph_id: str, article_id: str, paragraph_vector: list) -> None:\n",
    "        '''数据插入'''\n",
    "        paragraph_id = int(paragraph_id)\n",
    "        article_id = int(article_id)\n",
    "\n",
    "        collection = Collection(self.collection_name)  # Get an existing collection.\n",
    "        data = [[paragraph_id], [article_id], [0], [paragraph_vector]]\n",
    "        self.collection.insert(data)\n",
    "\n",
    "    async def get_top_paragraphs(self, Q_vector: list, articles: list) -> list:\n",
    "        '''通过问题与文章列表，检索相关段落(超过2篇文章)'''\n",
    "        ids = []\n",
    "        article_id_str = \"\"\n",
    "        for line in articles:\n",
    "            article_id_str += \" article_id == \" + str(line)\n",
    "        results = self.collection.search(\n",
    "            data=[list(Q_vector)],\n",
    "            anns_field=\"paragraph_vector\",\n",
    "            param=self.search_params,\n",
    "            limit=10,\n",
    "            expr=\"article_id == \" + str(line),\n",
    "            consistency_level=\"Strong\"\n",
    "        )\n",
    "        for line in results[0].ids:\n",
    "            ids.append(str(line))\n",
    "        return ids[:10]\n",
    "\n",
    "    async def get_comparison_paragraphs(self, Q_vector: list, articles: list) -> list:\n",
    "        '''通过问题与文章列表，检索相关段落(2篇文章)'''\n",
    "        ids = []\n",
    "        for line in articles:\n",
    "            results = self.collection.search(\n",
    "                data=[list(Q_vector)],\n",
    "                anns_field=\"paragraph_vector\",\n",
    "                param=self.search_params,\n",
    "                limit=10,\n",
    "                expr=\"article_id == \" + str(line),\n",
    "                consistency_level=\"Strong\"\n",
    "            )\n",
    "            ids.append(str(results[0].ids[0]))\n",
    "        return ids\n",
    "\n",
    "    def build(self) -> None:\n",
    "        '''构建集合'''\n",
    "        self.paragraph_id = FieldSchema(\n",
    "            name=\"paragraph_id\",\n",
    "            dtype=DataType.INT64,\n",
    "            is_primary=True\n",
    "        )\n",
    "\n",
    "        self.article_id = FieldSchema(\n",
    "            name=\"article_id\",\n",
    "            dtype=DataType.INT64\n",
    "        )\n",
    "\n",
    "        self.word_count = FieldSchema(\n",
    "            name=\"word_count\",\n",
    "            dtype=DataType.INT64\n",
    "        )\n",
    "        self.paragraph_vector = FieldSchema(\n",
    "            name=\"paragraph_vector\",\n",
    "            dtype=DataType.FLOAT_VECTOR,\n",
    "            dim=384\n",
    "        )\n",
    "        self.schema = CollectionSchema(\n",
    "            fields=[paragraph_id, article_id, word_count, paragraph_vector],\n",
    "            description=\"Paragraph search\"\n",
    "        )\n",
    "\n",
    "        self.collection = Collection(\n",
    "            name=self.collection_name,\n",
    "            schema=schema,\n",
    "            using='default',\n",
    "            shards_num=2,\n",
    "            consistency_level=\"Strong\"\n",
    "        )\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sentence_transformers import SentenceTransformer\n",
    "class Embedding:\n",
    "    def __init__(self):\n",
    "\n",
    "        self.model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')\n",
    "    def get_embedding(self, text: str) -> list:\n",
    "        '''文本转词向量'''\n",
    "        embeddings = self.model.encode(text)\n",
    "        return list(embeddings)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "ename": "FieldTypeException",
     "evalue": "<FieldTypeException: (code=0, message=The field of schema type must be FieldSchema.)>",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mFieldTypeException\u001b[0m                        Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-47-0a5e88802835>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m schema = CollectionSchema(\n\u001b[0m\u001b[1;32m      2\u001b[0m             \u001b[0mfields\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mparagraph_id\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0marticle_id\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mword_count\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mparagraph_vector\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      3\u001b[0m             \u001b[0mdescription\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m\"Paragraph_search\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      4\u001b[0m         )\n",
      "\u001b[0;32m~/.local/lib/python3.8/site-packages/pymilvus/orm/schema.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, fields, description, **kwargs)\u001b[0m\n\u001b[1;32m     37\u001b[0m         \u001b[0;32mfor\u001b[0m \u001b[0mfield\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_fields\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     38\u001b[0m             \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfield\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mFieldSchema\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 39\u001b[0;31m                 \u001b[0;32mraise\u001b[0m \u001b[0mFieldTypeException\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mExceptionsMessage\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mFieldType\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     40\u001b[0m             \u001b[0;32mif\u001b[0m \u001b[0mprimary_field\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0mfield\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     41\u001b[0m                 \u001b[0mfield\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mis_primary\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mFieldTypeException\u001b[0m: <FieldTypeException: (code=0, message=The field of schema type must be FieldSchema.)>"
     ]
    }
   ],
   "source": [
    "schema = CollectionSchema(\n",
    "            fields=[paragraph_id, article_id, word_count, paragraph_vector],\n",
    "            description=\"Paragraph_search\"\n",
    "        )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "collection = Collection(\n",
    "            name=\"article_paragraph\",\n",
    "            schema=schema,\n",
    "            using='default',\n",
    "            shards_num=2,\n",
    "            consistency_level=\"Strong\"\n",
    "        )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "search_params = {\"metric_type\": \"L2\", \"params\": {\"nprobe\": 10}}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[233524750408441189, 153546008563420080, 319681904401367092, 378752417796722145, 187528077588675447, 318345866768612330, 424144749677654774, 287193598808729433, 210437413246367943, 122571806592883084]"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# collection.load()\n",
    "results = collection.search(\n",
    "    data=[list(model.encode(\"你好\"))], \n",
    "    anns_field=\"paragraph_vector\", \n",
    "    param=search_params, \n",
    "    limit=10, \n",
    "    expr=\"article_id == 100000\",\n",
    "    consistency_level=\"Strong\"\n",
    ")\n",
    "results[0].ids"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<pymilvus.orm.search.SearchResult at 0x7fe17f4b97f0>"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results.on_result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "[\"\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Unexcepted error: [search], Field book_intro doesn't exist in schema, <Time: {'RPC start': '2023-10-12 11:59:26.755714', 'Exception': '2023-10-12 11:59:26.760200'}>\n"
     ]
    },
    {
     "ename": "ParamError",
     "evalue": "Field book_intro doesn't exist in schema",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mParamError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-30-0e6555894f48>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      6\u001b[0m   \u001b[0;34m\"expr\"\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;34m\"word_count <= 11000\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      7\u001b[0m }\n\u001b[0;32m----> 8\u001b[0;31m \u001b[0mres\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcollection\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msearch\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m**\u001b[0m\u001b[0msearch_param\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m~/.local/lib/python3.8/site-packages/pymilvus/orm/collection.py\u001b[0m in \u001b[0;36msearch\u001b[0;34m(self, data, anns_field, param, limit, expr, partition_names, output_fields, timeout, round_decimal, **kwargs)\u001b[0m\n\u001b[1;32m    688\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    689\u001b[0m         \u001b[0mconn\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_get_connection\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 690\u001b[0;31m         res = conn.search(self._name, data, anns_field, param, limit, expr,\n\u001b[0m\u001b[1;32m    691\u001b[0m                           partition_names, output_fields, timeout, round_decimal, **kwargs)\n\u001b[1;32m    692\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0mkwargs\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"_async\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;32mFalse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/.local/lib/python3.8/site-packages/pymilvus/decorators.py\u001b[0m in \u001b[0;36mhandler\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m     54\u001b[0m                     \u001b[0mtime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msleep\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mwait\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     55\u001b[0m                 \u001b[0;32mexcept\u001b[0m \u001b[0mException\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 56\u001b[0;31m                     \u001b[0;32mraise\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     57\u001b[0m                 \u001b[0;32mfinally\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     58\u001b[0m                     \u001b[0mcounter\u001b[0m \u001b[0;34m+=\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/.local/lib/python3.8/site-packages/pymilvus/decorators.py\u001b[0m in \u001b[0;36mhandler\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m     39\u001b[0m             \u001b[0;32mwhile\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     40\u001b[0m                 \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 41\u001b[0;31m                     \u001b[0;32mreturn\u001b[0m \u001b[0mfunc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     42\u001b[0m                 \u001b[0;32mexcept\u001b[0m \u001b[0mgrpc\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mRpcError\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     43\u001b[0m                     \u001b[0;31m# DEADLINE_EXCEEDED means that the task wat not completed\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/.local/lib/python3.8/site-packages/pymilvus/decorators.py\u001b[0m in \u001b[0;36mhandler\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m     84\u001b[0m             \u001b[0mrecord_dict\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"Exception\"\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdatetime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdatetime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnow\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     85\u001b[0m             \u001b[0mLOGGER\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0merror\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mf\"Unexcepted error: [{func.__name__}], {e}, <Time: {record_dict}>\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 86\u001b[0;31m             \u001b[0;32mraise\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     87\u001b[0m     \u001b[0;32mreturn\u001b[0m \u001b[0mhandler\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/.local/lib/python3.8/site-packages/pymilvus/decorators.py\u001b[0m in \u001b[0;36mhandler\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m     68\u001b[0m         \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     69\u001b[0m             \u001b[0mrecord_dict\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"RPC start\"\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdatetime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdatetime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnow\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 70\u001b[0;31m             \u001b[0;32mreturn\u001b[0m \u001b[0mfunc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     71\u001b[0m         \u001b[0;32mexcept\u001b[0m \u001b[0mBaseException\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     72\u001b[0m             \u001b[0mrecord_dict\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"RPC error\"\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdatetime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdatetime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnow\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/.local/lib/python3.8/site-packages/pymilvus/decorators.py\u001b[0m in \u001b[0;36mhandler\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m     19\u001b[0m             raise CollectionNotExistException(ErrorCode.CollectionNotExists,\n\u001b[1;32m     20\u001b[0m                                               f\"collection {collection_name} doesn't exist!\")\n\u001b[0;32m---> 21\u001b[0;31m         \u001b[0;32mreturn\u001b[0m \u001b[0mfunc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     22\u001b[0m     \u001b[0;32mreturn\u001b[0m \u001b[0mhandler\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     23\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/.local/lib/python3.8/site-packages/pymilvus/client/grpc_handler.py\u001b[0m in \u001b[0;36msearch\u001b[0;34m(self, collection_name, data, anns_field, param, limit, expression, partition_names, output_fields, timeout, round_decimal, **kwargs)\u001b[0m\n\u001b[1;32m    449\u001b[0m         \u001b[0mts_utils\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mconstruct_guarantee_ts\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mconsistency_level\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcollection_id\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0m_kwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    450\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 451\u001b[0;31m         requests = Prepare.search_requests_with_expr(collection_name, data, anns_field, param, limit, expression,\n\u001b[0m\u001b[1;32m    452\u001b[0m                                                      partition_names, output_fields, round_decimal, **_kwargs)\n\u001b[1;32m    453\u001b[0m         \u001b[0m_kwargs\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"schema\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/.local/lib/python3.8/site-packages/pymilvus/client/prepare.py\u001b[0m in \u001b[0;36msearch_requests_with_expr\u001b[0;34m(cls, collection_name, data, anns_field, param, limit, expr, partition_names, output_fields, round_decimal, **kwargs)\u001b[0m\n\u001b[1;32m    619\u001b[0m         \u001b[0mtag\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"$0\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    620\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0manns_field\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mfields_name_locs\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 621\u001b[0;31m             \u001b[0;32mraise\u001b[0m \u001b[0mParamError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mf\"Field {anns_field} doesn't exist in schema\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    622\u001b[0m         \u001b[0mdimension\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfields_schema\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mfields_name_locs\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0manns_field\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"params\"\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"dim\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    623\u001b[0m         \u001b[0mpls\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcls\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_prepare_placeholders\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnq\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmax_nq_per_batch\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtag\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mpl_type\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mis_binary\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdimension\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mParamError\u001b[0m: Field book_intro doesn't exist in schema"
     ]
    }
   ],
   "source": [
    "search_param = {\n",
    "  \"data\": [[0.1, 0.2]],\n",
    "  \"anns_field\": \"book_intro\",\n",
    "  \"param\": {\"metric_type\": \"L2\", \"params\": {\"nprobe\": 10}},\n",
    "  \"limit\": 2,\n",
    "  \"expr\": \"word_count <= 11000\",\n",
    "}\n",
    "res = collection.search(**search_param)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Milvus\n",
    "class Milvus:\n",
    "    HOST = 'localhost'\n",
    "    PORT = '19530'\n",
    "    ALIAS = 'default'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'localhost'"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Milvus.HOST"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "def concatenate_content(your_list):  \n",
    "    result = ''  \n",
    "    for item in your_list:  \n",
    "        if item.get('role') == 'user':  \n",
    "            result += item.get('content', '')  \n",
    "    return result  \n",
    "  \n",
    "# 使用示例  \n",
    "your_list = [  \n",
    "    {'role': 'user', 'content': 'Hello, '},  \n",
    "    {'role': 'bot', 'content': 'Hi, how can I help you?'},  \n",
    "    {'role': 'user', 'content': 'Can you help me with Python?'}  \n",
    "]  \n",
    "  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Hello, Can you help me with Python?'"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "concatenate_content(your_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
